package com.ly.sy.controller;


import com.aliyuncs.exceptions.ClientException;
import com.ly.sy.entity.PlayVideo;
import com.ly.sy.util.getPlayAuthorurl;
import com.ly.sy.entity.Video;
import com.ly.sy.service.VideoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ly.sy.util.getPlayAuthorurl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Controller
@RequestMapping("/sy/video")
public class VideoController {
    @Autowired
    private VideoService videoService;


//     根据课程id查询对应的视频
    @GetMapping("findVideoById")
    public String findVideoById (@RequestParam Integer courseId ,HttpSession session) throws ClientException {
        session.removeAttribute("aVideo");
        List<Video> videoList = videoService.BycourseId(courseId);

        if (!ObjectUtils.isEmpty(videoList)) {
            Video video = videoList.get(0);
            PlayVideo playVideo = new PlayVideo();
            playVideo.setCourseId(video.getCourseId());
            playVideo.setVideoId(video.getVideoId());
            playVideo.setVideoName(video.getVideoName());
            playVideo.setVideoUrl(video.getVideoUrl());
            playVideo.setVideoLength(video.getVideoLength());
            playVideo.setVideoAliUrl(getPlayAuthorurl.getPlayUrl(video.getVideoUrl()));
            playVideo.setVideoAuth(getPlayAuthorurl.getPlayAuthor(video.getVideoUrl()));
            session.setAttribute("aVideo", playVideo);
        }
        return "redirect:/sy/xktable/userBycourse?courseId="+courseId;

    }
//    显示课程id跳转到courseware页面
    @GetMapping("findVideoByIdtwo")
    public String findVideoByIdtwo (@RequestParam Integer courseId ,HttpSession session){
        session.removeAttribute("VideoList");
        List<Video> videoList = videoService.BycourseId(courseId);

        if (!ObjectUtils.isEmpty(videoList)) {

            session.setAttribute("VideoList", videoList);
        }
        return "User/courseware";

    }
    //    获取课程相关的评论信息并转跳到learning页面
    @GetMapping("learning")
    public String learning(@RequestParam Integer p, HttpSession session) throws ClientException {
        Video video = videoService.getById(p);
        PlayVideo playVideo = new PlayVideo();
        playVideo.setCourseId(video.getCourseId());
        playVideo.setVideoId(video.getVideoId());
        playVideo.setVideoName(video.getVideoName());
        playVideo.setVideoUrl(video.getVideoUrl());
        playVideo.setVideoLength(video.getVideoLength());
        playVideo.setVideoAliUrl(getPlayAuthorurl.getPlayUrl(video.getVideoUrl()));
        playVideo.setVideoAuth(getPlayAuthorurl.getPlayAuthor(video.getVideoUrl()));
        session.setAttribute("vname",playVideo);
        return "User/learning";
    }
//    跳转到courseware页面
    @GetMapping("goware")
    public String goware(){
        return "User/courseware";
    }
//    ****************************************admin*********************************************
    //视频管理界面
    @GetMapping("adminvideo")
    public String adminvideo(){
        return "admin/videoadmin";
    }
//跳转到添加视频界面
    @GetMapping("gobuilvideo")
    public String gobuilvideo(){
        return "admin/videoadd";
    }

//    视频上传
    @PostMapping("uploadvideo")
    @ResponseBody
    public String uploadvideo(
            @RequestParam("courseId") Integer courseId,
            @RequestParam("videoName") String videoName,
            @RequestParam("chapter") String chapter,
            @RequestParam("videoData") MultipartFile file,
            HttpSession session){

        if(ObjectUtils.isEmpty(file)){
            System.out.println("没有获取到文件");
            return "null";
        }
        System.out.println("获取到的video数据文件："+file);
        //视频上传,
        String videoId = videoService.uploadvideotoaliy(file);
        //数据传回数据库
        Video video = new Video();
        video.setCourseId(courseId);
        video.setVideoName(videoName);
        video.setVideoLength(chapter);
        video.setVideoUrl(videoId);
        videoService.save(video);
        System.out.println(videoId);
        return videoId;
    }
    /**
     * 获取实时长传进度
     * @param request
     * @return
     */
    @PostMapping("getUploadPercent")
    @ResponseBody
    public int getUploadPercent(HttpServletRequest request){
        HttpSession session = request.getSession();
        int percent = session.getAttribute("upload_percent") == null ? 0:  (Integer)session.getAttribute("upload_percent");
        return percent;
    }
    /**
     * 重置上传进度
     * @param request
     * @return
     */
    @PostMapping("resetPercent")
    public void resetPercent(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("upload_percent",0);
    }

    //根据视频课程id的到相关视频
    @GetMapping("videoList")
    @ResponseBody
    public String videoList(@RequestParam Integer courseid, HttpSession session){
        List<Video> video = videoService.BycourseId(courseid);
        session.setAttribute("courseOfV",video);
        session.setAttribute("couseidOfv",courseid);
        System.out.println("获取到了本课程相关视频");
        return "/sy/video/courseOfvideo";
    }
    @GetMapping("courseOfvideo")
    public String courseOfvideo(){
        return "/admin/courseOfvideo";
    }
    @GetMapping("appendvideo")
    public String appendvideo(@RequestParam Integer courseid, HttpSession session){
        session.setAttribute("couseidOfv",courseid);
        return "admin/appendvideo";
    }
//课程相关视频的搜索工能
    @GetMapping("search")
    @ResponseBody
    public List<Video> search(@RequestParam String searchstr, HttpSession session){
        Integer courseid= (Integer) session.getAttribute("couseidOfv");
        List<Video> videoList = videoService.videolikeLise(searchstr, courseid);
        return videoList;
    }
    //视频的删除
    @PostMapping("deletevideo")
    @ResponseBody
    public boolean deletevideo(Integer videoId){
        Video video = videoService.getById(videoId);
        //删除单条数据就给第二个参数，批量删除就给第一个参数
        getPlayAuthorurl.deteleVideo(null,video.getVideoUrl());
        //TODO
        //这里有一个更新session中的数据的步骤没做
        return videoService.removeById(videoId);
    }
    //编辑视频课件信息
    @GetMapping("goEditorvideo")
    @ResponseBody
    public String goEditorvideo(Integer videoid, HttpSession session){
        Video video = videoService.getById(videoid);
        session.setAttribute("editorvideo",video);
        return "/sy/video/EditorVideo";
    }
    @GetMapping("EditorVideo")
    public String EditorVideo(){
        return "/admin/EditorVideo";
    }
    @PostMapping("updateVideokById")
    public String updateVideokById(
            @RequestParam("courseId") Integer courseId,
            @RequestParam("videoName") String videoName,
            @RequestParam("chapter") String chapter,
            @RequestParam("videoId") Integer videoId,
            HttpSession session
    ){
        Video video = new Video();
        video.setVideoId(videoId);
        video.setCourseId(courseId);
        video.setVideoLength(chapter);
        video.setVideoName(videoName);
        videoService.updateById(video);
        Video editorvideo = videoService.getById(videoId);
        session.setAttribute("editorvideo",editorvideo);
        return "/admin/EditorVideo";
    }
}

